<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF 文字识别</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .upload-area {
            border: 2px dashed #ccc;
            padding: 20px;
            margin: 20px 0;
            border-radius: 5px;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        textarea {
            width: 100%;
            min-height: 300px;
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>PDF 文字识别</h1>
        <div class="upload-area">
            <input type="file" id="fileInput" accept="application/pdf">
            <p>请上传 PDF 文件</p>
        </div>
        <button id="extractBtn" class="button" style="display: none;">提取文字</button>
        <div id="pdfPreview" style="margin-top: 20px;"></div>
        <textarea id="outputText" placeholder="提取的文字将显示在这里..." readonly></textarea>
    </div>

    <script>
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;

            // 显示 PDF 预览
            const pdfPreview = document.getElementById('pdfPreview');
            pdfPreview.innerHTML = '';
            
            const iframe = document.createElement('iframe');
            iframe.src = URL.createObjectURL(file);
            iframe.width = '100%';
            iframe.height = '500px';
            iframe.style.border = '1px solid #ccc';
            
            pdfPreview.appendChild(iframe);
            
            // 显示提取按钮
            document.getElementById('extractBtn').style.display = 'inline-block';
        });

        document.getElementById('extractBtn').addEventListener('click', async function() {
            const file = document.getElementById('fileInput').files[0];
            if (!file) {
                alert('请先选择一个 PDF 文件');
                return;
            }

            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在提取...';

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/api/extract_pdf', {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                if (data.text) {
                    document.getElementById('outputText').value = data.text;
                } else {
                    alert('提取失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '提取文字';
            }
        });
    </script>
</body>
</html> 